<template>
	<view class="content">
		<view class="login-container" v-if="flag == 0">
			<image class="login-logo" src="@/static/user/logo-top.png"></image>
			<view class="login-select-xieyi">
				<view style="margin-left: 42rpx;">请点击登录授权后，点击使用头像，屏幕中确认提现</view>
			</view>
			 <button class="login-btn" open-type="chooseAvatar" @chooseavatar="doLogin">
			 	<!-- <image src="../../static/user/icon_wechat.png"></image> -->
			 	<view style="margin-left: 12rpx;">头像授权</view>
			 </button>
			 <view class="login-info">
			 	<view style="margin-left: 42rpx;">获取头像用于确认提现用户是您本人</view>
			 </view>
		</view>
		 <view class="login-container-t" v-if="flag == 1">
		 	<view class="tips-container">
		 		<view class="tips-top">请在【彩票机】上确认提现用户</view>
		 		<image src="@/static/user/img_tips.png" class="tips-image"></image>
		 		<view class="tips-content">
		 			<view >请确认屏幕是否您本人，如果是请点击【确定】,然后在手机领取奖金</view>
		 			<view >如果不是您本人的信息，您可以通过点击【取消】，重新进行扫码兑奖</view>
		 		</view>
		 	</view>
		 	<view class="tips-orderNo">订单号：{{orderNo}}</view>
		 	<view class="contact-server" @click="callKefu()">提现如有任何问题，请咨询客服电话：<a class="contact-span" >{{urlConfig.kfPhone}}</a></view>
		 </view>
	</view>
</template>

<script>
	import urlConfig from '../../common/config.js'
	export default {
		data() {
			return {
				orderStatus:0,
				txAmount:0,
				openId:'',
				orderNo:'',
				code:'',
				flag:0,
				openId:'',
				timer:null,
				qnToken:'',
				kfPhone:'',
			}
		},
		created() {
				this.$api.getQnToken({terminalNo:'ZA0591000101'}).then(res=>{
					this.qnToken = res;
				})
		},
		onLoad(option){
			this.orderNo = option.orderNo;
			let devNo = decodeURIComponent(option.q);
			if(devNo){
				if(devNo.indexOf("orderNo=") > -1){
					this.orderNo = devNo.slice(devNo.indexOf("orderNo=")+8);
				}
			}
			let that = this;
			uni.login({
				success: loginRes => {
					console.log(loginRes)
					that.code = loginRes.code
					that.$api.miniCodeLogin({departId:urlConfig.departId,code:loginRes.code}).then(res =>{
						
						this.openId = res.openId;
						console.log(this.openId);
						uni.setStorageSync('zfopenid',this.openId);
						if(res.avatarUrl){
							this.flag = 1;
							this.doAddHeadUrl(res.avatarUrl);
						}
					});
				}
			})
		},
		beforeDestroy() {
			if(this.timer){
				clearInterval(this.timer)
				this.timer = null;
			}
		},
		methods: {
			callKefu(){
				uni.makePhoneCall({
					phoneNumber: urlConfig.kfPhone
				});
			},
			doLogin(v){
				
				try{
					if(!v.detail.avatarUrl){
						uni.showToast({
							title:JSON.stringify(v),
							icon:'none'
						})
						return;
					}
				}catch(e){
					//TODO handle the exception
					uni.showToast({
						title:JSON.stringify(e.message),
						icon:'none'
					})
					return;
				}
				console.log(v);
				let ttime = new Date().getTime();
				uni.uploadFile({
					url: 'https://upload-z2.qiniup.com',
					filePath: v.detail.avatarUrl,
					name: 'file',
					formData: {
						'key': uni.getStorageSync('zfopenid')+'_'+ttime+'.png',
						'token': this.qnToken
					},
					success: (uploadFileRes) => {
						console.log(uploadFileRes)
						let headUrl = 'https://cpimage.chongdx.com/'+uni.getStorageSync('zfopenid')+'_'+ttime+'.png';
						this.doAddHeadUrl(headUrl);
					},
					fail: (err) => {
						console.log('fail', err);
						uni.redirectTo({
							url:'./cashmoney?orderNo='+this.orderNo+'&status=4'
						})
					}
				});
			},
			doAddHeadUrl(headUrl){
				this.$api.getWxUserInfo({departId:urlConfig.departId,
				orderNo:this.orderNo,
				openId:uni.getStorageSync('zfopenid'),avatarUrl:headUrl}).then(res =>{
					this.flag = 1;
					if(!this.timer){
						this.timer = setInterval(()=>{
							this.doCheck();
						}, 2000)
					}
				}).catch(err=>{
					uni.redirectTo({
						url:'./cashmoney?orderNo='+this.orderNo+'&status=99'
					})
				})
			},
			doCheck(){
				this.$api.checkSureTx({orderNo:this.orderNo}).then(res =>{
					if(res){
						if(this.timer){
							clearInterval(this.timer)
							this.timer = null;
						}
						if(res == '1'){
							uni.redirectTo({
								url: '../package/package?orderNo='+this.orderNo
							});
						}else{
							uni.redirectTo({
								url: './cashmoney?orderNo='+this.orderNo+'&status='+res
							});
						}
					}
				})
			},
			
		}
	}
</script>

<style lang="scss">
	// 需要
	uni-page-body,page {
		height:100%
	}
	.content{
		width: 100%;
		height:100%;
		display: flex;
		flex-direction: column;
		background-color: #F7F7F7;
		align-items: center;
	}
	.login-container{
		width: 100%;
		height:100%;
		display: flex;
		flex-direction: column;
		background-color: #F7F7F7;
		align-items: center;
		.login-logo{
			width: 422rpx;
			height: 106rpx;
			margin-top: 295rpx;
		}
		.login-select-xieyi{
			display: flex;
			width: 100%;
			flex-direction: row;
			margin-top: 107rpx;
			image {
				margin-left: 30rpx;
				width: 36rpx;
				height: 36rpx;
			}
			view {
				font-size: 30rpx;
				color: #575B66;
			}
			.click-font{
				color: #00C790;
			}
		}
		.login-btn{
			margin-top: 40rpx;
			width: 690rpx;
			height: 96rpx;
			background-color: #00C790;
			border-radius: 48rpx;
			display: flex;
			flex-direction: row;
			justify-content: center;
			align-items: center;
			image{
				width: 46rpx;
				height: 41rpx;
			}
			view {
				color: white;
				font-size: 34rpx;
			}
		}
	
		.tips-container{
			width: 650rpx;
			height: 820rpx;
			background-color: #fff;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			margin-top: 134rpx;
			.tips-top{
				width: 650rpx;
				height: 86rpx;
				background-color: #fff6dc;
				display: flex;
				justify-content: center;
				align-items: center;
				color: #DC1616;
				font-size: 34rpx;
				font-weight: bold;
			}
			.tips-image{
				width: 362rpx;
				height: 406rpx;
				margin-top: 52rpx;
			}
			.tips-content{
				width: 570rpx;
				height: auto;
				padding: 30rpx;
				background: rgba(0,0,0,0.61);
				margin-top: 32rpx;
				color: #FFFFFF;	
				font-size: 26rpx;
			}
		}
		.tips-orderNo{
			font-size: 26rpx;
			width: 570rpx;
			text-align: left;
			display: flex;
			justify-content: flex-start;
			align-items: center;
			color: #333;
			margin-top: 12rpx;
		}
		.contact-server{
			
			text-align: center;
			color: #333;
			font-size: 36rpx;
			margin-top: 50rpx;
			font-weight: bold;
		}
		.contact-span{
			color: #ff0000;
		}
	}
	.login-info{
		display: flex;
		width: 100%;
		flex-direction: row;
		view{
			margin-top: 20rpx;
			font-size: 26rpx;
			color: #575B66;
		}
	}
	.login-container-t{
		width: 100%;
		height:100%;
		display: flex;
		flex-direction: column;
		background-color: #F7F7F7;
		align-items: center;
		.login-logo{
			width: 422rpx;
			height: 106rpx;
			margin-top: 295rpx;
		}
		.login-select-xieyi{
			display: flex;
			width: 100%;
			flex-direction: row;
			margin-top: 107rpx;
			image {
				margin-left: 30rpx;
				width: 36rpx;
				height: 36rpx;
			}
			view {
				font-size: 26rpx;
				color: #575B66;
			}
			.click-font{
				color: #00C790;
			}
		}
		.login-btn{
			margin-top: 40rpx;
			width: 690rpx;
			height: 96rpx;
			background-color: #00C790;
			border-radius: 48rpx;
			display: flex;
			flex-direction: row;
			justify-content: center;
			align-items: center;
			image{
				width: 46rpx;
				height: 41rpx;
			}
			view {
				color: white;
				font-size: 34rpx;
			}
		}
	
		.tips-container{
			width: 650rpx;
			height: 820rpx;
			background-color: #fff;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			margin-top: 134rpx;
			.tips-top{
				width: 650rpx;
				height: 86rpx;
				background-color: #fff6dc;
				display: flex;
				justify-content: center;
				align-items: center;
				color: #DC1616;
				font-size: 34rpx;
				font-weight: bold;
			}
			.tips-image{
				width: 362rpx;
				height: 406rpx;
				margin-top: 52rpx;
			}
			.tips-content{
				width: 570rpx;
				height: auto;
				padding: 30rpx;
				background: rgba(0,0,0,0.61);
				margin-top: 32rpx;
				color: #FFFFFF;	
				font-size: 26rpx;
			}
		}
		.tips-orderNo{
			font-size: 26rpx;
			width: 570rpx;
			text-align: left;
			display: flex;
			justify-content: flex-start;
			align-items: center;
			color: #333;
			margin-top: 12rpx;
		}
		.contact-server{
			
			text-align: center;
			color: #333;
			font-size: 36rpx;
			margin-top: 50rpx;
			font-weight: bold;
		}
		.contact-span{
			color: #ff0000;
		}
	}
	

</style>
